/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package proy2redes;
/**
 *
 * @author jose
 */
//import java.io.*;
import java.util.*;
import java.io.*;
import nanoxml.*;


public class Main {

    /**
     * @param args the command line arguments
     */

    private static String HOSTDREP = "localhost";
    private static String PUERTODREP = "31109";
    private static String LDIRECTORIO = ".";
    private static String NOMBRE = "guest";
    public static HashMap organizarEntrada(String[] args){
        HashMap map = new HashMap();
        
        int tam = args.length;
        System.out.println("OrganizarEntrada " +tam);
        if (tam%2!=0) {
            return null;
        }
        int i = 0;
        while (i<tam){
            map.put(args[i], args[i+1]);
            i = i+2;
        }
        System.out.println("Tamano del Map: "+map.size());
        return map;
    }

    public static boolean comprobarEntrada(String[] args,String[] values){
        boolean valido = false;
        HashMap map = organizarEntrada(args);
        if (map==null){
            return false;
        }

        if (map.containsKey("-h")){
            values[0] = (String)map.get("-h");
            map.remove("-h");
        }
        if (map.containsKey("-p")){
            values[1] = (String)map.get("-p");
            map.remove("-p");
        }
        if (map.containsKey("-d")){
            values[2] = (String)map.get("-d");
            map.remove("-d");
        }
        if (map.containsKey("-a")){
            values[3] = (String)map.get("-a");
            map.remove("-a");
        }
        if (map.containsKey("-n")){
            values[4] = (String)map.get("-n");
            map.remove("-n");
        }
        if (map.containsKey("-c")){
            values[5] = (String)map.get("-c");
            map.remove("-c");
        }
        if (map.isEmpty())
            valido = true;
        
        return valido;
    }

    public static HashMap<String,String> organizarEntradaXML(String arch) {
        HashMap<String,String> map = null;
        try{
            XMLElement xml = new XMLElement();
            FileReader reader = new FileReader(arch);
            xml.parseFromReader(reader);
            System.out.println(xml);
            System.out.println(xml.countChildren());
            Enumeration enum2 = xml.enumerateChildren();
            while(enum2.hasMoreElements()){
                XMLElement child = (XMLElement)enum2.nextElement();
                map.put(child.getName(), child.getContent());
                System.out.println(child.getName()+" "+child.getContent());
            }
        } catch (java.io.FileNotFoundException fne){
            System.out.println("Error: No existe el archivo de "
                    + "configuracion "+arch);
        } catch (IOException ioe){
            System.out.println("Error al leer el archivo de "
                    + "configuracion.\n"+ioe);
        }
        catch (nanoxml.XMLParseException xpe) {

        }
        return map;
    }

    public static void valoresEntradaXML(String arch,String[] values){
        //boolean valido = false;
        HashMap map = organizarEntradaXML(arch);
        if (map==null){
            return;
        }

        if (map.containsKey("hostserv")){
            values[0] = (String)map.get("hostserv");
            map.remove("hostserv");
        }
        if (map.containsKey("puertoserv")){
            values[1] = (String)map.get("puertoserv");
            map.remove("puertoserv");
        }
        if (map.containsKey("directorio")){
            values[2] = (String)map.get("directorio");
            map.remove("directorio");
        }
        if (map.containsKey("archivo")){
            values[3] = (String)map.get("archivo");
            map.remove("archivo");
        }
        if (map.containsKey("nombre")){
            values[4] = (String)map.get("nombre");
            map.remove("nombre");
        }
        if (!map.isEmpty())
            System.out.println("Advertencia: El archivo "
                    + "posee elementos no identificados.");

        //return valido;
    }

    public static void main(String[] args) {
        // TODO code application logic here
        /*String n = "/home/jose/DocumentosarchX/pjava";
        //String c = manejoArchivos.contenido(n);
        //System.out.println(c);
        String s = manejoArchivos.listarDirectorio(n);
        System.out.println(s);
*/
        System.out.println("Tamano de args: "+args.length);
        String host = "";
        Integer port = 0;//Integer.parseInt(host);
        String direct = "";
        String arch = "";
        String name = "";
        String[] valores = new String[6];
        valores[0] = HOSTDREP;
        valores[1] = PUERTODREP;
        valores[2] = LDIRECTORIO;
        valores[3] = null;
        valores[4] = NOMBRE;
        valores[5] = null;
        boolean b = Main.comprobarEntrada(args,valores);
        String archConf = valores[5];
        if (archConf != null){
            valoresEntradaXML(archConf,valores);
        }
        System.out.println(b+" "+args[1]);
        System.out.println("Valores asignados:\n Host: "+valores[0]+" Puerto: "+
                valores[1]);

        /*try{

            String archX = "/home/jose/Documentos/Redes/lab3/proy3/config.xml";
            XMLElement xml = new XMLElement();
            FileReader reader = new FileReader(archX);
            xml.parseFromReader(reader);
            System.out.println(xml);
            System.out.println(xml.countChildren());
            Enumeration enum2 = xml.enumerateChildren();
            while(enum2.hasMoreElements()){
                XMLElement child = (XMLElement)enum2.nextElement();
                System.out.println(child.getName()+" "+child.getContent());
            }
            //XMLElement ppal = (XMLElement)xml.getAttribute("configuracion");
            //System.out.println("valor: "+ppal.getStringAttribute("hostserv"));
        } catch (Exception e){
            System.out.println(e);
        }*/
    }

}
